﻿using System;
using System.Collections.Generic;
using System.Text;


using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;

namespace Future.WcfClient
{
    public class WcfClient : ClientBase<Future.Contract.IWcfService>
    {
        public WcfClient(Binding binding, EndpointAddress address)
            : base(binding, address)
        {

        }

        private void ChannelSet()
        {
            this.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 0);
        }

        bool useByte = true;
        public bool ExecCommand(ref Channel.Task task, ref string errMsg)
        {
            bool result = true;

            ChannelSet();

            Channel.Task taskres = null;
            if (useByte)
            {
                byte[] buffer = this.ExecCommandByte(task, ref result, ref errMsg);
                taskres = Future.Common.ZipTools.DecompressionObject<Channel.Task>(buffer);
            }
            else
            {
                taskres = LitJson.JsonMapper.ToObject<Channel.Task>(this.ExecCommandTask(task, ref result, ref errMsg));
            }

            task = taskres;
            return result;
        }

        private byte[] ExecCommandByte(Channel.Task task, ref bool result, ref string errMsg)
        {
            return this.Channel.ExecCommandByte(task, ref result, ref errMsg);
        }

        private string ExecCommandTask(Channel.Task task, ref bool result, ref string errMsg)
        {
            return this.Channel.ExecCommandString(task, ref result, ref errMsg);
        }

        public int XPing(int n)
        {
            return this.Channel.XPing(n);
        }
    }
}
